package com.imc.platform.base.pay.http.tuple;

import com.alibaba.fastjson.annotation.JSONField;
import com.imc.core.utils.MD5Utils;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;

import java.util.Date;
import java.util.UUID;

/**
 * Created by tanweiping on 2018/9/4.
 */
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Data
public class PayDto {
    /*
    小程序ID 	appId 	是 	String 	wxd678efh567hg6787 	微信分配的小程序ID
    时间戳 	timeStamp 	是 	String 	1490840662 	时间戳从1970年1月1日00:00:00至今的秒数,即当前的时间
    随机串 	nonceStr 	是 	String 	5K8264ILTKCH16CQ2502SI8ZNMTM67VS 	随机字符串，不长于32位。推荐随机数生成算法
    数据包 	package 	是 	String 	prepay_id=wx2017033010242291fcfe0db70013231072 	统一下单接口返回的 prepay_id 参数值，提交格式如：prepay_id=wx2017033010242291fcfe0db70013231072
    签名方式 	signType 	是 	String 	MD5Utils 	签名类型，默认为MD5，支持HMAC-SHA256和MD5。注意此处需与统一下单的签名类型一致

    举例如下:

    paySign = MD5Utils(appId=wxd678efh567hg6787&nonceStr=5K8264ILTKCH16CQ2502SI8ZNMTM67VS&package=prepay_id=wx2017033010242291fcfe0db70013231072&signType=MD5Utils&timeStamp=1490840662&key=qazwsxedcrfvtgbyhnujmikolp111111) = 22D9B4E54AB1950F51E0649E8810ACD6
         */
    private String appId;
    private String timeStamp;
    private String nonce_str;

    private String mPackage;

    @Builder.Default
    private String signType = "MD5";

    private String prepay_id;

    private String paySign;

    public String getNonce_str() {
        if (StringUtils.isEmpty(nonce_str)){
            nonce_str = UUID.randomUUID().toString().substring(0,16);
        }
        return nonce_str;
    }

    public String getTimeStamp() {
        if (StringUtils.isEmpty(timeStamp)){
            timeStamp = String.valueOf(new Date().getTime());
        }
        return timeStamp;
    }

    public String getmPackage() {
        return "prepay_id="+this.getPrepay_id();
    }

    public String getMyPaySign(String key) {
        String stringA = "appId="+getAppId()+"&nonceStr="+getNonce_str()
                +"&package="+this.getmPackage()+"&signType="+signType+"&timeStamp="+this.getTimeStamp();
        String stringSignTemp=stringA+"&key="+key; //注：key为商户平台设置的密钥key
        return MD5Utils.MD5Encode(stringSignTemp,"UTF-8");
    }
}
